<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// 应用公共文件
function ajaxArr($code, $msg='', $data=[], $count = 0){
    $res['code'] = $code;
    $res['msg'] = $msg;
    $res['data'] = $data;
    if ($count){
        $res['count'] = $count;
    }
    return $res;
}
function pwdMd5($pwd,$salt){
    return md5(md5($pwd).$salt);
}

function arr($data){
    echo '<pre>';print_r($data);
}
//判断权限是否选中
function checkRule($id,$rule){
    return in_array($id,explode(',',$rule));
}

function getTimeInfo($time){
    $now_time = time();
    if($time>$now_time-3600){
        return ceil(($now_time-$time)/60).'分钟前';
    }elseif($time>$now_time-86400){
        return ceil(($now_time-$time)/3600).'小时前';
    }elseif($time>$now_time-86400*7){
        return ceil(($now_time-$time)/86400).'天前';
    }
    return date('Y-m-d',$time);
}

function diffBetweenTwoDays ($day1, $day2){
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return ($second1 - $second2) / 86400 + 1;
}

/**
 * 递归生成树状数组
 * @param $data
 * @param $pId
 * @param string $pkey
 * @param string $cKey
 * @return array|string
 */
function getTree($data, $pId, $pkey='id', $cKey='pid')
{
    $tree = [];
    foreach($data as $k => $v)
    {
        if($v[$cKey] == $pId)
        {         //父亲找到儿子
            $v['child'] = getTree($data, $v[$pkey]);
            $tree[] = $v;
            //unset($data[$k]);
        }
    }
    return $tree;
}

/**
 * 返回图片路径
 * @param $host
 * @return string
 */
function getImgUrl($host=""){
    $config = \think\facade\Cache::get('config_temp');
    $imgHost = $config['img_url'] ? $config['img_url'] : $_SERVER['HTTP_HOST'];
    $host = $host?$host:$imgHost;
    $ssl = $config['is_ssl'] == 1 ? 'https://' : 'http://';
    return $ssl . $host . '/uploads';
}

/**
 * 返回密码盐
 * @return string
 */
function getSalt(){
    return '_' . rand(1000, 9999);
}


/**
 * 调试使用,输出数组
 * @param unknown $array
 */
function p($array) {
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}

/**
 * 生成随机字符串
 * @param string $type 类型
 * @param number $len 长度
 * @return string
 */
function randomString($type = 'alnum', $len = 8) {
    switch ($type) {
        case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 'numeric' : $pool = '0123456789';
            break;
        case 'nozero' : $pool = '123456789';
            break;
    }

    $str = '';
    for ($i = 0; $i < $len; $i++) {
        $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
    }
    return $str;
}

function getParents($categorys,$parentId = '')
{
    $tree = [];
    if($categorys){
        foreach ($categorys as $item){
            if($parentId != 0){
                if($item['categoryId'] == $parentId){
                    $tree[] = $item;
                    $tree = array_merge($tree,$this->getAccordOe($data,$item['parentId']));
                }
            }
        }

        return $tree;
    }
}



